exports.StoreContainer = GL2.Node.subclass(
{
    classname: 'StoreContainer',
    
    initialize: function(storeModel) 
    {
    	this._storeModel = null;
		this._storeUIArray = null;
		this._upgradeTimeout = null;
		this._restockTimeout = null;
		this._coinIcon = null;
		this._tutorialIndicator = null;
		
		if(storeModel)
		{
			this._storeModel = storeModel;
		}
		this._storeUIArray = new GAMutableArray();
    },
	
	createView: function()
	{	
		var itemModel = this._storeModel.getItemModel();
		var itemSizeWidth = itemModel.custom_attributes.width;
		
		for(var i=0 ; i<itemSizeWidth ; i++)
		{
			var storeUI = new StoreUI(this._storeModel, (this._storeModel.getPosition().getY()+i));
			storeUI.setStorePosition();
			storeUI.createView();
			storeUI.setDepth(9999);
			this.addChild(storeUI);	
			this._storeUIArray.addObject(storeUI);
		}
		
		this.checkAndAddCoinIcon();
		this.checkForUpgrade();
		this.checkForRestock();
	},
	
	startProgressBarForUpgrade: function(barType)
	{
		this._progressBarForUpgrade = new ProgressBar(this._storeModel, barType);//kProgressBarType.kProgressBarTypeUpgrade
		this._progressBarForUpgrade.createView();
		this._progressBarForUpgrade.setBarPosition();
		this._progressBarForUpgrade.setDepth(99999);
		this._progressBarForUpgrade.startProgress();
		this.addChild(this._progressBarForUpgrade);
	},
	
	startProgressBarForRestock: function(barType)
	{
		this._progressBarForRestock = new ProgressBar(this._storeModel, barType);//kProgressBarType.kProgressBarTypeUpgrade
		this._progressBarForRestock.createView();
		this._progressBarForRestock.setBarPosition();
		this._progressBarForRestock.setDepth(99999);
		this._progressBarForRestock.startProgress();
		this.addChild(this._progressBarForRestock);
	},
	
	removeProgressBarForUpgrade: function()
	{
		if(this._progressBarForUpgrade)
		{ 
			this._progressBarForUpgrade.destroy();
			this._progressBarForUpgrade = null;
		}
	},
	
	removeProgressBarForRestocking: function()
	{
		if(this._progressBarForRestock)
		{
			this._progressBarForRestock.destroy();
			this._progressBarForRestock = null;
		}
	},
	
	setContainerPosition: function()
	{
		var gridX = this._storeModel.getPosition().getX();
		var positionX = -(FLOOR_HEIGHT + (+FLOOR_HEIGHT * +gridX));
		this.setPosition(TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(positionX));
	},
		
	checkAndAddCoinIcon: function()
	{	
		{
			if(!this._storeModel.getIsStockEmpty() && !this._storeModel.isUpgradeInProgress() && !this._storeModel.isRestockInProgress() && !this._coinIcon)
			{	
				var itemID = this._storeModel.getItemId();
				if(itemID != "1_1_0")
				{		
					if(this.isRewardAvaliable())
					{
						if(this._checkAndAddCoinIconTimer)
						{
							clearTimeout(this._checkAndAddCoinIconTimer);
							this._checkAndAddCoinIconTimer = null;
						}
						this.addCoinIcon();
					}
					else
					{
						var remainingTimeForReward = this.remainingTimeForReward();		
						if(remainingTimeForReward < 0)
						{
							remainingTimeForReward = 0;
						}
						this._checkAndAddCoinIconTimer = setTimeout(this.checkAndAddCoinIcon.bind(this), remainingTimeForReward*1000);
					}
				}
			}
		}
	},
	
	checkForUpgrade: function()
	{
		var storeModel = this.getModelObject(); 
		if(storeModel.isUpgradeInProgress())
		{
			var upgradeRemainingTime = storeModel.upgradeRemainingTime();
			if(upgradeRemainingTime > 0)
			{
				this.setRelevantImages();
				this._upgradeTimeout = setTimeout(this.upgradeDoneCallBack.bind(this), upgradeRemainingTime*1000);
				this.startProgressBarForUpgrade(kProgressBarType.kProgressBarTypeUpgrade);
			}
			else
			{
				this.upgradeDoneCallBack();
			}
		}
	},
	
	checkForRestock: function()
	{
		var storeModel = this.getModelObject(); 
		if(storeModel.isRestockInProgress())
		{
			var restockRemainingTime = storeModel.restockRemainingTime();
			if(restockRemainingTime > 0)
			{
				this.setRelevantImages();
				this._restockTimeout = setTimeout(this.restockDoneCallBack.bind(this), restockRemainingTime*1000);
				this.startProgressBarForRestock(kProgressBarType.kProgressBarTypeRestock);
			}
			else
			{
				this.restockDoneCallBack();
			}
		}
		
		if(this._storeModel.getIsStockEmpty())
		{
			this.setRelevantImages();
		}
	},
		
	addCoinIcon: function()
	{
		var storeUIPosition = null
		if(this._storeUIArray.getLength() == 1)
		{
			storeUIPosition = this._storeUIArray.objectAtIndex(0).getPosition();
		}
		else 
		{
			storeUIPosition = this._storeUIArray.objectAtIndex(1).getPosition();
		}
		
		var coinImagePath = this.getBaseResourcePath() + "coin.png";
		this._coinIcon = new GLButton(coinImagePath, coinImagePath, TCUtil.getAdjustedX(60),TCUtil.getAdjustedY(60), this.coinIconTapped.bind(this), this, 10000, "", null, null, null, null);
		this._coinIcon.setPosition(storeUIPosition.getX() + TCUtil.getAdjustedX(40), storeUIPosition.getY() + TCUtil.getAdjustedY(20));
		MMSoundManager.playEffect(COIN_PRODUCED_SOUND);
	},
	
	showTutorialForCoin: function()
	{
		var position = this._coinIcon.getPosition();
		this._tutorialIndicatorForCoin = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForCoin.setPosition([position.getX() + TCUtil.getAdjustedX(17), position.getY() + TCUtil.getAdjustedY(320)]);
		this._tutorialIndicatorForCoin.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForCoin);
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(75),TCUtil.getAdjustedY(150)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_1"));
	},
	
	showTutorialForRestocking: function()
	{		
		this._tutorialIndicatorForRestock = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForRestock.setPosition([TCUtil.getAdjustedX(95), TCUtil.getAdjustedY(365)]);
		this._tutorialIndicatorForRestock.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForRestock);
		
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(35),TCUtil.getAdjustedY(160)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_2"));
	},
	
	showTutorialForInstantRestocking: function()
	{
		this._tutorialIndicatorForInstantRestock = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForInstantRestock.setPosition([TCUtil.getAdjustedX(95), TCUtil.getAdjustedY(365)]);
		this._tutorialIndicatorForInstantRestock.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForInstantRestock);	
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(50),TCUtil.getAdjustedY(160)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_3"));			
	},
	
	showTutorialForUpgrade: function()
	{
		this._tutorialIndicatorForUpgrade = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForUpgrade.setPosition([TCUtil.getAdjustedX(95), TCUtil.getAdjustedY(365)]);
		this._tutorialIndicatorForUpgrade.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForUpgrade);		
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(35),TCUtil.getAdjustedY(160)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_4"));
	},
	
	showTutorialForInstantUpgrade: function()
	{
		this._tutorialIndicatorForInstantUpgrade = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForInstantUpgrade.setPosition([TCUtil.getAdjustedX(95), TCUtil.getAdjustedY(365)]);
		this._tutorialIndicatorForInstantUpgrade.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForInstantUpgrade);
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(35),TCUtil.getAdjustedY(160)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_5"));
	},
	
	showTutorialForFriendCoin: function()
	{
		var position = this._coinIcon.getPosition();
		this._tutorialIndicatorForFriendCoin = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForFriendCoin.setPosition([position.getX() + TCUtil.getAdjustedX(17), position.getY() + TCUtil.getAdjustedY(320)]);
		this._tutorialIndicatorForFriendCoin.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForFriendCoin);
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(75),TCUtil.getAdjustedY(150)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_12_D"));
	},
	
	coinIconTapped: function()
	{
		if(DatabaseInterface._isVisitingNeighbor)
		{
			if(TutorialManager.getHasCompletedTutorial())
			{
				var friend = MMFriendManager.getVisitingFriendObj();
				var serverTime = MMGameState.getServerTime();
				var currentActions = MMFriendActionManager.getFriendCoinActionState(friend.id);
				if(currentActions)
				{				
					var timeToReset = parseInt(currentActions[1]) + SECS_IN_ONE_DAY - parseInt(serverTime);
					var actionCount = +currentActions[2];
					
					if(timeToReset < 0)
					{
						actionCount = 0;
					}
					
					if(actionCount >= MAX_FRIEND_COIN_COLLECT)
					{
						var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), GALocalizationHandler.getRelativeString(Core.Localization.getString("REACHED_LIMIT_FOR_HELPING_FRIEND"),[friend.displayName]), ["Ok"], this);
						return;
					}
					else
					{
						//var rewardAmount = this._storeModel.getItemModel().custom_attributes.reward;
						var rewardAmount = REWARD_FOR_FRIEND_COIN;
						MMFriendActionManager.setFriendCoinCollectedCount(+rewardAmount);
						actionCount++;
						MMFriendActionManager.saveFriendCoinActionState([""+friend.id,serverTime,actionCount]);
					}
				}
				else
				{
					MMFriendActionManager.saveFriendCoinActionState([""+friend.id,serverTime,1]);
				}
			}
			var rewardAmount = REWARD_FOR_FRIEND_COIN;
			MMFriendActionManager.setFriendCoinCollectedCount(+rewardAmount);
		}
		
		if(this._coinIcon)
		{		
			this._coinIcon.destroy();
			this._coinIcon = null;
		}
		if(this._tutorialIndicatorForCoin)
		{
			this._tutorialIndicatorForCoin.destroy();
			this._tutorialIndicatorForCoin = null;
			TutorialManager.showNextTutorialStep();
		}
		if(this._tutorialIndicatorForFriendCoin)
		{
			this._tutorialIndicatorForFriendCoin.destroy();
			this._tutorialIndicatorForFriendCoin = null;
			MMGameLayer.getControlLayer().getBottomBar().showTutorialForFriendTrash();
		}
		
		MMSoundManager.playEffect(COIN_COLLECT_SOUND);
		
		var rewardAmount = this._storeModel.getRewardAmount();
		
		this._storeModel.setLastCollected(MMGameState.getServerTime());
		
		this._storeModel.setNumberOfCollections(+this._storeModel.getNumberOfCollections() + 1);
		
		MMGameState.getCurrentUser().updateCurrency(kOperationType.kOperationAddCoins, rewardAmount);//this calls updateModel in itself so no need here..
		
		if(this._storeModel.getIsStockEmpty() || this._storeModel.isUpgradeInProgress() || this._storeModel.isRestockInProgress())
		{
			if(this._checkAndAddCoinIconTimer)
			{
				clearTimeout(this._checkAndAddCoinIconTimer);
				this._checkAndAddCoinIconTimer = null;
			}
			this.setRelevantImages();
		}
		else
		{		
			var remainingTimeForReward = this.remainingTimeForReward();		
			if(remainingTimeForReward < 0)
			{
				remainingTimeForReward = 0;
			}
			this._checkAndAddCoinIconTimer = setTimeout(this.checkAndAddCoinIcon.bind(this), remainingTimeForReward*1000);
		}
	},
	
	isRewardAvaliable: function()
	{
		var itemModel = this._storeModel.getItemModel();
		var itemID = this._storeModel.getItemId();
		var currentTime = MMGameState.getServerTime();
		var upgradeState = this._storeModel.getUpgradeState();
		var rewardCollectionTime = itemModel.custom_attributes.collection_time[upgradeState].time;
		var lastRewardCollectedTime = this._storeModel.getLastCollected();
				
		if((currentTime - lastRewardCollectedTime) < rewardCollectionTime || itemID == "1_1_0" || (!TutorialManager.getHasCompletedTutorial() && TutorialManager.getCurrentTutorialStep() > 1 && TutorialManager.getCurrentTutorialStep() < 12))
		{
			return false;
		}		
		return true;
		},
		
	remainingTimeForReward: function()
	{
		var itemModel = this._storeModel.getItemModel();
		var currentTime = MMGameState.getServerTime();
		var upgradeState = this._storeModel.getUpgradeState();		
		var rewardCollectionTime = itemModel.custom_attributes.collection_time[upgradeState].time;
		var lastRewardCollectedTime = this._storeModel.getLastCollected();		
		var remainingTimeForReward = (lastRewardCollectedTime + rewardCollectionTime) - currentTime;
						
		return remainingTimeForReward;
	},
	
	isRestockingNeeded: function()
	{
		var itemModel = this._storeModel.getItemModel();		
		var numberOfCollections = this._storeModel.getNumberOfCollections();
		var totalNumberOfCollections = itemModel.custom_attributes.no_of_collections;
		
		if(numberOfCollections < totalNumberOfCollections)
		{
			return true;
		}
		return false;
	},
	
	isConstructionCompleted: function()
	{
		var itemModel = this._storeModel.getItemModel();		
		var currentTime = MMGameState.getServerTime();
		var totalConstructionTime = itemModel.custom_attributes.construction_time;
		var constructionStartTime = this._storeModel.getConstructionStartTime();
		
		if((currentTime - constructionStartTime) > totalConstructionTime)
		{
			return true;
		}
		return false;
	},	
	
	storeTapped: function(storeUI)
	{
		if(storeUI)
		{
			MMGameLayer.getMallUI().handleStoreTapAction(storeUI.getModelObject().getPosition());
		}
	},
	
	getStoreUIArray: function()
	{
		return this._storeUIArray;
	},
	
	upgradeStore: function()
	{
		if(this._checkAndAddCoinIconTimer)
		{
			clearTimeout(this._checkAndAddCoinIconTimer);
			this._checkAndAddCoinIconTimer = null;
		}
		var storeModel = this.getModelObject();
		if(!storeModel.canUpgradeStore())
    	{
    		return;
    	} 
    	
    	var pricing = storeModel.getUpgradePricing();
		if(storeModel.canUpgradeBePurchased(pricing.currency, pricing.cost))
		{
			this.startProgressBarForUpgrade(kProgressBarType.kProgressBarTypeUpgrade);
			MMSoundManager.playEffect(NEW_AND_UPGRADE_SOUND);
			storeModel.upgradeStore();
			this.setRelevantImages();
			var upgradeRemainingTime = storeModel.upgradeRemainingTime();
			this._upgradeTimeout = setTimeout(this.upgradeDoneCallBack.bind(this), upgradeRemainingTime*1000);
			if(pricing.currency == kCurrencyType.kCurrencyTypeCoins)
			{
				AnalyticsHandler.logAnalyticsOffer("Upgrade", this._storeModel.getUpgradePricing() , "Coins",this._storeModel.getItemId());
			}
			else
			{
				AnalyticsHandler.logAnalyticsOffer("Upgrade", this._storeModel.getUpgradePricing() , "Nuggets",this._storeModel.getItemId());
			}
		}
		else
		{
			//TODO Put Alert something to show user that he/she dont have enough currency to do action
			var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), Core.Localization.getString("NOT_ENOUGH_MONEY"), ["ok"], this);
		}
		if(!TutorialManager.getHasCompletedTutorial())
    	{
    		if(TutorialManager.getCurrentTutorialStep() == 4)
    		{
    			if(this._tutorialIndicatorForUpgrade)
    			{
    				this._tutorialIndicatorForUpgrade.destroy();
    				this._tutorialIndicatorForUpgrade = null;
    			}
				TutorialManager.showNextTutorialStep();
    		}
    	}
	},
	
	restockStore: function()
	{
		if(this._checkAndAddCoinIconTimer)
		{
			clearTimeout(this._checkAndAddCoinIconTimer);
			this._checkAndAddCoinIconTimer = null;
		}		
		var storeModel = this.getModelObject();
		if(!storeModel.canRestockStore())
    	{
    		return;
    	} 
    	var pricing = storeModel.getRestockPricing();
		if(storeModel.canRestockBePurchased(kCurrencyType.kCurrencyTypeCoins, pricing))
		{
			//MMSoundManager.playEffect(NEW_AND_UPGRADE_SOUND);
			this.startProgressBarForRestock(kProgressBarType.kProgressBarTypeRestock);
			storeModel.restockStore();
			this.setRelevantImages();
			var restockRemainingTime = storeModel.restockRemainingTime();
			this._restockTimeout = setTimeout(this.restockDoneCallBack.bind(this), restockRemainingTime*1000);
		}
		else
		{
			//TODO Put Alert something to show user that he/she dont have enough currency to do action
			var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), Core.Localization.getString("NOT_ENOUGH_MONEY"), ["ok"], this);
		}
		
		if(!TutorialManager.getHasCompletedTutorial())
    	{
    		if(TutorialManager.getCurrentTutorialStep() == 2)
    		{
    			this._tutorialIndicatorForRestock.destroy();
    			this._tutorialIndicatorForRestock = null;
				TutorialManager.showNextTutorialStep();
    		}
    	}
		
	},	
	
	setRelevantImages: function()
	{
		for(var i=0 ; i<this._storeUIArray.getLength() ; i++)
		{
			var storeUI = this._storeUIArray.objectAtIndex(i);
			storeUI.setRelevantImage();
		}
	},	
	
	upgradeDoneCallBack: function()
	{
		this.removeProgressBarForUpgrade();
    	if(!TutorialManager.getHasCompletedTutorial())
    	{
    		if(TutorialManager.getCurrentTutorialStep() == 5)
    		{
    			if(this._tutorialIndicatorForInstantUpgrade)
    			{
	    			this._tutorialIndicatorForInstantUpgrade.destroy();
	    			this._tutorialIndicatorForInstantUpgrade = null;	
    			}
    			TutorialManager.showNextTutorialStep();
    		}	
    	}
		this.getModelObject().upgradeCompleted();//UpdateModel
		
		this.checkAndAddCoinIcon();
		for(var i=0 ; i<this._storeUIArray.getLength() ; i++)
		{
			var storeUI = this._storeUIArray.objectAtIndex(i);
			storeUI.setRelevantImage();
			//storeUI.getModelObject().upgradeCompleted();
		}
	},
	
	restockDoneCallBack: function()
	{
		this.removeProgressBarForRestocking();
    	if(!TutorialManager.getHasCompletedTutorial())
    	{
    		if(TutorialManager.getCurrentTutorialStep() == 3)
    		{
    			if(this._tutorialIndicatorForInstantRestock)
    			{
	    			this._tutorialIndicatorForInstantRestock.destroy();
	    			this._tutorialIndicatorForInstantRestock = null;	
    			}
    			TutorialManager.showNextTutorialStep();
    		}	
    	}		
		this.getModelObject().restockCompleted();//UpdateModel is done in restockCompleted
		//this._storeModel.updateModel();
		this.checkAndAddCoinIcon();
		
		for(var i=0 ; i<this._storeUIArray.getLength() ; i++)
		{
			var storeUI = this._storeUIArray.objectAtIndex(i);
			storeUI.setRelevantImage();
			//storeUI.getModelObject().upgradeCompleted();
		}
	},	
	
	getModelObject: function()
	{
		return this._storeModel;
	},
	
	getBaseResourcePath: function()
	{
		return "./Content/images/gamelayer/environmentlayer/storeui/";
	},
	
	getThumbImagePath: function()
	{
		var tempPath = this._storeModel.getItemId().split("_")
		return "./Content/images/storeimages/" + tempPath[0] + "/" + tempPath[1] + "/" + tempPath[2] + "/" +"default/thumb.png";
	},
								   
	destroy: function()
    {
    	this.removeProgressBarForUpgrade();
    	this.removeProgressBarForRestocking();
    	if(this._checkAndAddCoinIconTimer)
		{
			clearTimeout(this._checkAndAddCoinIconTimer);
			this._checkAndAddCoinIconTimer = null;
		}
		if(this._upgradeTimeout)
		{
			clearTimeout(this._upgradeTimeout);
			this._upgradeTimeout = null;
		}
		if(this._restockTimeout)
		{
			clearTimeout(this._restockTimeout);
			this._restockTimeout = null;
		}
		if(this._storeModel)
		{
			this._storeModel.destroy();
			this._storeModel = null;
		}
		if(this._storeUIArray)
		{
			this._storeUIArray.destroyWithObjects();
		}
		if(this._coinIcon)
		{		
			this._coinIcon.destroy();
			this._coinIcon = null;
		}
    },
});
